<?php
class ArticleAction extends CommonAction {
	
	function _initialize(){
		parent::_initialize();
	}
	
    //过滤查询字段
    function _filter(&$map){
        if(!empty($_POST['name'])) {
       		$map['title'] = array('like',"%".$_POST['name']."%");
        }
    }
    
    function index(){
    	$key = $this->_param('name', 'trim');
    	$map = array();
    	if($key)
    		$map['title'] = array('like', '%'.$key.'%');
    	
    	$model = D('Article');
    	$count = $model->where($map)->count('id');
    	
    	if ($count > 0) {
    		import("ORG.Util.Page");
    		
    		//分页查询数据
    		$p = new Page($count, 20);
    		$voList = $model->where($map)->order("`id` desc")->limit($p->firstRow . ',' . $p->listRows)->select();
    		
    		//分页跳转的时候保证查询条件
    		foreach ($map as $key => $val) {
    			if (!is_array($val)) {
    				$p->parameter .= "$key=" . urlencode($val) . "&";
    			}
    		}
    		
    		//分页显示
    		$page = $p->show();
    		//模板赋值显示
    		$this->assign('list', $voList);
    		$this->assign("page", $page);
    	}
    	cookie('_currentUrl_', __SELF__);
    	$this->display();
    }
    
    function add(){
    	if($this->isPost()){
	    	$model = D('Article');
	    	if (false === $model->field('title,content')->create()) {
	    		$this->error($model->getError());
	    	}
	    	//$model->add_time = date('YmdHis');
	    	//保存当前数据对象
	    	$list = $model->add();
	    	if ($list !== false) { //保存成功
	    		$this->success('新增成功!',cookie('_currentUrl_'));
	    	} else {
	    		//失败提示
	    		$this->error('新增失败!'.$model->getDbError());
	    	}
    	}
    	else{
    		$this->display();
    	}
    }
    
    function edit(){
    	$id = $this->_request('id');
    	
    	if($id == '')
    		$this->error('参数错误!');
    	
	    $model = D('Article');
    	$info = $model->find($id);
    	
    	if($this->isPost()){
	    	if (false === $model->field('title,content')->create()) {
	    		$this->error($model->getError());
	    	}
	    	//$model->edit_time = date('YmdHis');
	    	//保存当前数据对象
	    	$list = $model->add();
	    	if ($list !== false) { //保存成功
	    		$this->success('编辑成功!',cookie('_currentUrl_'));
	    	} else {
	    		//失败提示
	    		$this->error('编辑失败!');
	    	}
    	}
    	else{
    		$this->assign('vo', $info);
    		$this->display();
    	}
    }
    
    function delete(){
    	$id = $this->_request('id');
    	 
    	if($id == '')
    		$this->error('参数错误!');
    	 
    	$model = D('Article');
//     	$flag = $model->where(array('id'=>$id))->delete();
    	$flag = $model->delete($id);
    	
    	if(!$flag)
    		$this->error('删除失败!');
    	else
    		$this->success('删除成功!',cookie('_currentUrl_'));
    	
    }
    
    
}